package interfacePackageStructures;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;

import mapPackage.IRoute;
import mapPackage.IVille;
import mapPackage.MapException;
import mapPackage.Route;
import mapPackage.Ville;
import mapPackage.tailleVille.STailleVille;
import mapPackage.typeRoute.STypeRoute;

import xmlPackage.Carte;

public class MyTableRoutes extends AbstractTableModel {
	
	private String[] columnNames = {"Nom",
                                    "Type", 
                                    "Ouvert"};
	
	ArrayList<String> data = new ArrayList<String>();
	
	public MyTableRoutes(char lettre) {
		System.out.println("MyTableRoutes lettre " + lettre);
		
		String test = lettre + "";
		data.clear();
		
		Iterator<String> routesIterator = Carte.GetInstance().getRoutesIterator(test.toUpperCase());
		while (routesIterator.hasNext()) {
			String nomRoute = routesIterator.next();
			data.add(nomRoute);
		}
		routesIterator = Carte.GetInstance().getRoutesIterator(test.toLowerCase());
		while (routesIterator.hasNext()) {
			String nomRoute = routesIterator.next();
			data.add(nomRoute);
		}
		
		fireTableDataChanged();
	}

	public void supprimerRoute(int numRow) {
		String nomRoute = data.get(numRow);
		data.remove(nomRoute);
		try {
			Carte.GetInstance().supprimerRoute(nomRoute);
		} catch (MapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		fireTableRowsDeleted(numRow, numRow);
	}
	
	public String getRouteNom(int rowNum) {
		String nomRoute = data.get(rowNum);
		return nomRoute;
	}
	
	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return data.size();
	}

    public String getColumnName(int col) {
        return columnNames[col];
    }

	public Object getValueAt(int row, int col) {
		try {
			String nomRoute = data.get(row);
			IRoute route  = Carte.GetInstance().getRoute(nomRoute);
			//System.out.println("getValueAt row " + row + " col " + col);
	    	
			switch (col) {
			case 0:
				return route.getNom();
			case 1:
				return route.getType();
			case 2:
				return route.isOuverte();
			default:
				return "";
			}
			
		} catch (MapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        return true;
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {           

    	try {
			System.out.println("setValueAt row " + row + " col " + col);
	    	//si la ligne existe pas on la cree sinon modif
	    	if ((data.size() <= row) || (data.size() == 0 )){
	    			//data.add(new VilleIntermediaire((String)value));
	    		
	    	}
	    	else {
	    		String nomRoute = data.get(row);
				IRoute route = Carte.GetInstance().getRoute(nomRoute);
				
				switch (col) {
				case 0:
					route.setNom((String)value);
					break;
				case 1:
					route.setType((STypeRoute)value);
					break;
				case 2:
					route.setOuvert((Boolean)value);
					break;
				default:
				}
				route.setModified(true);
	    		fireTableCellUpdated(row, col);
	    	}

		} catch (MapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public void ajouterRoute(String nom, STypeRoute type, boolean ouverte) {
		try {
			Carte.GetInstance().ajouterRoute(new Route(nom));
			IRoute route = Carte.GetInstance().getRoute(nom);
			route.setType(type);
			route.setOuvert(ouverte);
			route.setModified(true);
	    	data.add(nom);
	    	fireTableRowsInserted(data.size() - 1, data.size() - 1);
		} catch (MapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
